package com.navinfo.opentsp.platform.computing.analysis.util;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2018/5/21.
 */
public class NumberFormatUtil {

    public static ThreadLocal<DecimalFormat> decimalFormatLocal = new ThreadLocal<DecimalFormat>(){
        @Override
        protected synchronized DecimalFormat initialValue() {
            DecimalFormat format = new DecimalFormat("##.##");
            return format;
        }
    };

    private static DecimalFormat getFormatter(){
        DecimalFormat format = decimalFormatLocal.get();
        return format;
    }

    private static DecimalFormat getFormatter(String formatPattern){
        DecimalFormat format = decimalFormatLocal.get();
        format.applyPattern(formatPattern);
        return format;
    }

    public static String format(Object o){
        return getFormatter().format(o);
    }

    public static String format(Object o, String pattern){
        return getFormatter(pattern).format(o);
    }

    public static void main(String[] args) {
        float a = 1223.042323f;
        System.out.println(NumberFormatUtil.format(a));
    }
}
